<%--
  User: Sujan
  Date: Apr 27, 2007
  Time: 6:30:25 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.scalar.chiptrack.commons.Constants,
                 com.scalar.chiptrack.utils.ConfigManager"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<chiptrack:checkLogon sourcePage="/creditNoteAction.do" />
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<HTML>
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet"></HEAD>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/quickSearch.js"></script>

<SCRIPT language="JavaScript">
    var lineItemsCount = 0;
    var dateElement = "";
    function openCalendar(dateValue)
    {
        dateElement = dateValue;
        newWindow = window.open('<%=chipTracContext%>/jsp/includes/calendar.html','calenderPopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=180,height=440,resizable=no');
        newWindow.moveTo(550,180);
        newWindow.focus();
    }

    function assignDate(passedValue)
    {
        dateElement.value=passedValue;
        window.focus();
    }
            
    function reset()
    {
        document.creditNoteForm.reset();
    }
    function submitPage()
    {
        var form =  document.creditNoteForm;
        if ( validateForm() )
        {
            enableFormFields();
            form.action = "<%=chipTracContext%>/creditNoteAction.do?action=<%=Constants.ACTION_UPDATE%>"
            form.submit();
        }
    }
    function selectReason( reasonSelectCtrl, idx )
    {
        var form = document.creditNoteForm;
        if ( reasonSelectCtrl.value == -1 )
        {
            form["lineItems["+idx+"].reason"].readOnly = false;
            form["lineItems["+idx+"].reason"].value = "";
            form["lineItems["+idx+"].reason"].focus();
        }
        else
        {
            form["lineItems["+idx+"].reason"].value = reasonSelectCtrl.value;
            form["lineItems["+idx+"].reason"].readOnly = true;
        }
    }

    function validateForm()
    {
        var form = document.creditNoteForm;
        var pa = document.getElementById("errors");
        pa.innerHTML = "";
        var errors = "";
        if (! chkDate(form.issueDate.value.trim(), "Issue Date", "Y"))
        {
            errors += errMsg;
            form.issueDate.select();
            form.issueDate.focus();
        }

        //At lease one line item should be selected
        for (var k = 0; k < lineItemsCount; k++)
        {
            if ( form['lineItems[' + k + '].status'].value != 'APPROVED')
            {
                if (form['lineItems[' + k + '].reason'].value == '')
                {
                    errors += "<li>Please select Reason"
                    form['lineItems[' + k + '].selectReason'].focus();
                    break;
                }
                //checking amounts
                var adjustmentQtyCtrl = form['lineItems[' + k + '].adjustmentQty'];
                var adjustmentPriceCtrl = form['lineItems[' + k + '].discountPrice'];
                var adjustmentAmountCtrl = form['lineItems[' + k + '].discountAmount'];

                if (adjustmentQtyCtrl.value != '')
                {
                    if (!isInteger(adjustmentQtyCtrl.value))
                    {
                        errors += "<li>Please enter a valid quantity ";
                        adjustmentQtyCtrl.select();
                        break;
                    }
                    else  if ((adjustmentQtyCtrl.value).length > 6)
                    {
                        errors += "<li>Please enter a valid quantity (should not allow more than 6 digits)";
                        adjustmentQtyCtrl.select();
                        break;
                    }
                }
                if ( adjustmentPriceCtrl.value != '' && adjustmentPriceCtrl.value != 0 )
                {
                    if (! checkFloat(adjustmentPriceCtrl.value, ' Adjustment Price ', 'N'))
                    {
                        errors += "<li>" + errMsg + " or characters";
                        adjustmentPriceCtrl.focus();
                        break;
                    }
                }
                if ( adjustmentAmountCtrl.value != '' && adjustmentAmountCtrl.value != 0 )
                {
                    if (! checkFloat(adjustmentAmountCtrl.value, ' Adjustment Amount ', 'N'))
                    {
                        errors += "<li>" + errMsg + " or characters";
                        adjustmentAmountCtrl.focus();
                        break;
                    }
                }
                if (adjustmentAmountCtrl.value == '' || adjustmentAmountCtrl.value <= 0)
                {
                    errors += "<li>Please enter either Adjustment Qty And Price OR enter only Adjustment Amount";
                    adjustmentAmountCtrl.select();
                    adjustmentAmountCtrl.focus();
                    break;
                }
            }
        }
        if (errors != '')
        {
            pa.innerHTML = errors;
            return false;
        }
        return true;
    }
    function adjustAmount(lineItemIdx)
    {
        var form = document.creditNoteForm;
        var adjustmentQtyCtrl = form['lineItems[' + lineItemIdx + '].adjustmentQty'];
        var adjustmentPriceCtrl = form['lineItems[' + lineItemIdx + '].discountPrice'];
        var adjustmentAmountCtrl = form['lineItems[' + lineItemIdx + '].discountAmount'];
        var adjustmentAmount = adjustmentQtyCtrl.value * adjustmentPriceCtrl.value;
        if ( adjustmentAmount.toFixed )
        {
            adjustmentAmountCtrl.value = adjustmentAmount.toFixed(3);
        }
        else
        {
             adjustmentAmountCtrl.value = adjustmentAmount;
        }
        calculateTotalAdjustmentAmt();
    }
    function resetQtyPrice( lineItemIdx )
    {
        var form = document.creditNoteForm;
        var adjustmentQtyCtrl = form['lineItems[' + lineItemIdx + '].adjustmentQty'];
        var adjustmentPriceCtrl = form['lineItems[' + lineItemIdx + '].discountPrice'];
        adjustmentQtyCtrl.value = 0;
        adjustmentPriceCtrl.value = 0.0;
        calculateTotalAdjustmentAmt();
    }
    function enableDisableLineItem( lineItemIdx )
    {
        var form = document.creditNoteForm;
        if ( form['lineItems[' + lineItemIdx + '].isCancel'].checked )
        {
            form['lineItems[' + lineItemIdx + '].selectReason'].disabled = true;
            form['lineItems[' + lineItemIdx + '].adjustmentQty'].disabled = true;
            form['lineItems[' + lineItemIdx + '].discountPrice'].disabled = true;
            form['lineItems[' + lineItemIdx + '].discountAmount'].disabled = true;
        }
        else
        {
            form['lineItems[' + lineItemIdx + '].selectReason'].disabled = false;
            form['lineItems[' + lineItemIdx + '].adjustmentQty'].disabled = false;
            form['lineItems[' + lineItemIdx + '].discountPrice'].disabled = false;
            form['lineItems[' + lineItemIdx + '].discountAmount'].disabled = false;
        }

    }
    function enableFormFields ()
    {
        var form = document.creditNoteForm;
        for (var k = 0; k < lineItemsCount; k++)
        {
            form['lineItems[' + k + '].selectReason'].disabled = false;
            form['lineItems[' + k + '].adjustmentQty'].disabled = false;
            form['lineItems[' + k + '].discountPrice'].disabled = false;
            form['lineItems[' + k + '].discountAmount'].disabled = false;
        }
    }
    function calculateTotalAdjustmentAmt()
    {
        var form = document.creditNoteForm;
        var totalAdjustmentAmt = 0.0;
        for (var k = 0; k < lineItemsCount; k++)
        {
            var adjustmentAmt = form['lineItems[' + k + '].discountAmount'].value;
            if ( adjustmentAmt != '' )
                totalAdjustmentAmt = eval(totalAdjustmentAmt)+eval(adjustmentAmt);
        }
        if ( totalAdjustmentAmt.toFixed )
        {
            form.totalAdjustmentAmount.value = totalAdjustmentAmt.toFixed(3);
        }
        else
        {
            form.totalAdjustmentAmount.value = totalAdjustmentAmt;
        }
    }
</SCRIPT>

<BODY bgColor="#ffffff" leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">

<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
    <TR>
	    <TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
	    <!-- Side Navigation start -->
	    <%@include file="/jsp/finance/finance_menu.jsp" %>
	    <!-- end of the side navigations -->
        </TD>
	    <TD bgcolor="#ffffff"><IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
	    <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
	    <TD valign="top" width="100%"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
	    <BR>
        <html:form action="/creditNoteAction.do" name="creditNoteForm" type="com.scalar.chiptrack.finance.creditNote.forms.CreditNoteForm" onsubmit="return false;">
        <nested:hidden property="noteType"/>
        <nested:hidden property="creditNoteId"/>
        <%
            String action = request.getParameter ( "action" );
        %>
            <TABLE width="95%" align="right" cellpadding="2" cellspacing="2" border="0">
			    <TR><TD colspan="5" class="NArialW" align="center">
                    <logic:equal value="Credit" name="creditNoteForm" property="noteType">
                        Modify Credit Note
                    </logic:equal>
                    <logic:notEqual value="Credit" name="creditNoteForm" property="noteType">
                        Modify Debit Note 
                    </logic:notEqual>
                </TD></TR>
                <TR><TD colspan='5' align="left" class='NArialL'>&nbsp;<FONT id="errors" color='FF0000'></font></TD></TR>
			    <TR><TD colspan='5' class="NArialL"><chiptrack:errors styleClass="NArialL"/></TD></TR>
			    <TR><TD colspan='5' class="NArialL"><chiptrack:info styleClass="NArialL"/></TD></TR>
			    <TR><TD colspan='5'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></TD></TR>
                <tr>
                    <td class="NArialL">
                    <logic:equal value="Credit" name="creditNoteForm" property="noteType">
                        Credit Note #:
                    </logic:equal>
                    <logic:notEqual value="Credit" name="creditNoteForm" property="noteType">
                        Debit Note #:
                    </logic:notEqual>

                    </td>
                    <td class="NArialL">
                        <nested:write property="creditNoteNumber" />
                        <nested:hidden property="creditNoteNumber" />
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL">
                        Date Issued:
                    </td>
                    <td class="bodytext">
                            <nested:text property="issueDate" size="12" styleClass="txfield" />
                            <a href="javascript:openCalendar(document.creditNoteForm.issueDate)"><img onMouseOver="window.status='Click here to select date';return true" border="0" align='middle' src="<%=chipTracContext%>/jsp/includes/images/date_selector.gif" height=20 width=20></a>
                    </td>
                </tr>
                <tr>
                    <td class="NArialL">
                        Customer:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="customerName" />
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL">
                        Sales Order No:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="salesOrderNo" />
                    </td>
                </tr>
                <tr>
                    <td class="NArialL">
                        Customer PO No:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="customerPONo" />
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL">
                        Sales Rep:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="salesRep" />
                    </td>
                </tr>
                <tr>
                    <td class="NArialL">
                        End Customer:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="endCustomerName" />
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL">
                        End User:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="endUserName" />
                    </td>
                </tr>
                <tr>
                    <td class="NArialL">
                        Pay Terms:
                    </td>
                    <td class="bodytext">
                        <bean:write name="creditNoteForm" property="payTerms" />
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL">
                       Packing Slip Label:
                    </td>
                    <td class="bodytext">
                       <bean:write name="creditNoteForm" property="packingSlipLabel" />
                    </td>
                </tr>
                <tr>
                    <td class="NArialL" valign="top">
                        Billing Address:
                    </td>
                    <td class="bodytext" valign="top">
                        <bean:write name="creditNoteForm" property="billingAddressAsHTML" filter="false"/>
                    </td>
                    <td>&nbsp;</td>
                    <td class="NArialL" valign="top">
                       Shipping Address:
                    </td>
                    <td class="bodytext" valign="top">
                       <bean:write name="creditNoteForm" property="shippingAddressAsHTML" filter="false"/>
                    </td>
                </tr>
                <tr>
                    <td class="NArialL">Consider Units:</td>
                    <td class="bodytext" valign="top">
                        <nested:equal property="isUnits" value="true">Yes</nested:equal>
                        <nested:notEqual property="isUnits" value="true">No</nested:notEqual>
                    </td>
                </tr>
                <tr>
                    <td width="11%" class="NArialL">Comments:</td>
                    <td width="89%" colspan="5">
                        <html:textarea name="creditNoteForm" property="comments" rows="4" cols="100" styleClass="txfield" onkeydown="javascript:setTextAreaMaxLength(document.creditNoteForm.comments,300)" onkeyup="javascript:setTextAreaMaxLength(document.creditNoteForm.comments,300)"/>
                    </td>
                </tr>
                <nested:hidden property="salesOrderId"/>
                <TR><TD colspan='5'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></TD></TR>
                
                <tr>
                    <td colspan=5>
                        <table border="0" width="100%">
                            <tr>
                                <td colspan="15">
                                        <font class="NArialS" color='blue'>
                                            <b><li>&nbsp;Disabled rows indicate APPROVED items and they cannot be changed.</li></b>
                                        </font>
                                    <br>
                                </td>
                            </tr>
                            <tr>
                                <td width="8%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Line Item #</font></td>
	                            <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">PO Line Item #</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.mfgPartNumber" /></font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF"><bean:message key="label.customerPartNumber"/></font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Description</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Shipped Qty</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Ship Date</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Unit Price</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Amount</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Select Reason</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Reason</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Change To Qty</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Change To Price($)</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Change To Amount($)</font></td>
                                <td width="10%" align="center" bgcolor="#993333" class="NArialL"><font color="#FFFFFF">Cancel</font></td>

                            </tr>
                            
                            <nested:iterate property="lineItems" indexId="idx" id="lineItem">
                            <%
                                boolean disableLineItem = false;
                            %>
                            <tr>
                                <nested:equal value="APPROVED" property="status">
                                    <%
                                        disableLineItem = true;
                                    %>
                                </nested:equal>
                                <nested:hidden property="lineItemId"/>
                                <nested:hidden property="isCreditNote" />
                                <nested:hidden property="status"/>
                                <td width="8%" bgcolor="#DEE1E2" align=center class="NArialL"> <nested:write property="lineItemNo" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="poLineItemNo" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="manufacturingPartNo" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="customerPartNo" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="lineItemDescription" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="shippedQtyAsString" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="shippedDate" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="unitPriceAsString" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:write property="amountAsString" /></td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL">
                                    <select class="txfield" name="lineItems[<%=idx.intValue()%>].selectReason" onchange="selectReason(this, <%=idx.intValue()%>)" <%=disableLineItem?"disabled":""%> >
                                        <option value="" selected></option>
                                        <option value="Wrong Price">Wrong Price</option>
                                        <option value="RMA">RMA</option>
                                        <option value="Dist Discount">Dist Discount</option>
                                        <option value="-1">Others</option>
                                    </select>
                                </td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:text property="reason" size="15" styleClass="txfield" readonly="true" /></td>
                                <%
                                    String adjustAmount = "adjustAmount(" + idx.intValue() + ")";
                                    String resetQtyPrice = "resetQtyPrice(" + idx.intValue() + ")";
                                    String enableDisableLineItem = "enableDisableLineItem(" + idx.intValue() + ")";
                                %>
                                <td width="10%" bgcolor="#DEE1E2" align=right class="NArialL" nowrap>
                                    <logic:equal value="Credit" name="creditNoteForm" property="noteType">-</logic:equal><nested:text property="adjustmentQty" onkeypress="javascript:allowInteger()" size="7" maxlength="6" styleClass="valuetxfield" disabled="<%=disableLineItem%>" onchange="<%=adjustAmount%>"/>
                                </td>
                                <td width="10%" bgcolor="#DEE1E2" align=right class="NArialL" nowrap>
                                    <nested:text property="discountPrice" onkeypress="javascript:restrictCharacters(this,7,3)" maxlength="9" size="9" styleClass="valuetxfield" disabled="<%=disableLineItem%>" onchange="<%=adjustAmount%>"/>
                                </td>
                                <td width="10%" bgcolor="#DEE1E2" align=right class="NArialL" nowrap>
                                    <logic:equal value="Credit" name="creditNoteForm" property="noteType">-</logic:equal><nested:text property="discountAmount" onkeypress="javascript:restrictCharacters(this,10,3)" size="10" styleClass="valuetxfield" disabled="<%=disableLineItem%>" onchange="<%=resetQtyPrice%>"/>
                                </td>
                                <td width="10%" bgcolor="#DEE1E2" align=center class="NArialL"><nested:checkbox property="isCancel" styleClass="txfield" disabled="<%=disableLineItem%>" onclick="<%=enableDisableLineItem%>"/></td>
                            </tr>
                            </nested:iterate>
                            <nested:size id="lineItemsCount" property="lineItems"/>
                            <script type="text/javascript">
                                lineItemsCount = <%=lineItemsCount.intValue()%>;
                            </script>
                            <tr>
                                <td colspan="13" class="NArialL" align="right">Total:</td>
                                <td colspan="2" nowrap><logic:equal value="Credit" name="creditNoteForm" property="noteType">-</logic:equal><nested:text property="totalAdjustmentAmount" styleClass="valuetxfield" disabled="true" size="10"/></td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr><td>&nbsp;</td></tr>
                <tr>
                    <td colspan="5" align="center">
                        <a href="javascript:submitPage()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onmouseover="window.status='Click to Search Credit Note';return true" onmouseout="window.status='';return true" title="Click here to Search Credit Note" alt=""></a>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:reset();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"></a>
                    </td>
			    </tr>
			    <TR><TD colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></TD></TR>
		    </TABLE>
	</html:form>
<!-- till here -->
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>

<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->

</BODY>
</HTML>